package com.hustmed.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hustmed.domain.ProcessParams;
import com.hustmed.domain.Sample;
import com.hustmed.mapper.ProcessParamsMapper;
import com.hustmed.mapper.SampleMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hustmed.mapper.ExperimentMapper;
import com.hustmed.domain.Experiment;
import com.hustmed.service.IExperimentService;

/**
 * 实验Service业务层处理
 *
 */
@Service
public class ExperimentServiceImpl implements IExperimentService 
{
    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private SampleMapper sampleMapper;

    @Autowired
    private ProcessParamsMapper processParamsMapper;

    /**
     * 查询实验
     * 
     * @param id 实验主键
     * @return 实验
     */
    @Override
    public Experiment selectExperimentById(String id)
    {
        return experimentMapper.selectExperimentById(id);
    }

    /**
     * 查询实验列表
     * 
     * @param experiment 实验
     * @return 实验
     */
    @Override
    public List<Experiment> selectExperimentList(Experiment experiment)
    {
        List<Experiment> experimentList = experimentMapper.selectExperimentList(experiment);
        Map<String,Experiment> eMap = new HashMap<String,Experiment>();
        for(Experiment i : experimentList){
            if(null == eMap.get(i.getId())){
                eMap.put(i.getId(),i);
                i.setProcessMap(new HashMap<String,ProcessParams>());
                i.setProcessParamsList(new ArrayList<ProcessParams>());
                if(StringUtils.isNotEmpty(i.getProcessParamsId())){
                    ProcessParams pp = new ProcessParams();
                    pp.setId(i.getProcessParamsId());
                    pp.setProcessParamsNo(i.getProcessParamsNo());
                    pp.setVjson(i.getPvjson());
                    pp.setCjson(i.getPcjson());
                    pp.setSampleMap(new HashMap<String,Sample>());
                    pp.setSampleList(new ArrayList<Sample>());
                    i.getProcessParamsList().add(pp);
                    i.getProcessMap().put(pp.getId(),pp);
                    if(StringUtils.isNotEmpty(i.getSampleId())){
                        Sample s = new Sample();
                        s.setCjson(i.getScjson());
                        s.setVjsonq(i.getSvjson());
                        s.setId(i.getSampleId());
                        s.setSampleNo(i.getSampleNo());
                        s.setName(i.getSampleName());
                        s.setRemark(i.getSampleRemark());
                        pp.getSampleList().add(s);
                        pp.getSampleMap().put(i.getSampleId(),s);
                    }
                }
            }else{
                Experiment e = eMap.get(i.getId());
                HashMap<String,ProcessParams>pMap = e.getProcessMap();
                if(StringUtils.isNotEmpty(i.getProcessParamsId())){
                    if(null == pMap.get(i.getProcessParamsId())){
                        ProcessParams pp = new ProcessParams();
                        pp.setSampleMap(new HashMap<String,Sample>());
                        pp.setSampleList(new ArrayList<Sample>());
                        pp.setId(i.getProcessParamsId());
                        pp.setProcessParamsNo(i.getProcessParamsNo());
                        pp.setVjson(i.getPvjson());
                        pp.setCjson(i.getPcjson());
                        e.getProcessMap().put(i.getProcessParamsId(),pp);
                        if(StringUtils.isNotEmpty(i.getSampleId())){
                            Sample s = new Sample();
                            s.setCjson(i.getScjson());
                            s.setVjsonq(i.getSvjson());
                            s.setId(i.getSampleId());
                            s.setSampleNo(i.getSampleNo());
                            s.setName(i.getSampleName());
                            s.setRemark(i.getSampleRemark());
                            pp.getSampleMap().put(s.getId(),s);
                            pp.getSampleList().add(s);
                        }
                        e.setProcessMap(pMap);
                        e.getProcessParamsList().add(pp);
                    }else{
                        if(StringUtils.isNotEmpty(i.getSampleId())){
                            ProcessParams pp = pMap.get(i.getProcessParamsId());
                            HashMap<String,Sample> sMap = pp.getSampleMap();
                            if(null == sMap.get(i.getSampleId())){
                                Sample s = new Sample();
                                s.setCjson(i.getScjson());
                                s.setVjsonq(i.getSvjson());
                                s.setId(i.getSampleId());
                                s.setSampleNo(i.getSampleNo());
                                s.setName(i.getSampleName());
                                s.setRemark(i.getSampleRemark());
                                sMap.put(i.getSampleId(),s);
                                pp.getSampleList().add(s);
                            }
                        }
                    }
                }
            }
        }
        List<Experiment> eList = new ArrayList<Experiment>(eMap.values());
        eList.sort(
            new Comparator<Experiment>() {
                @Override
                public int compare(Experiment o1, Experiment o2) {
                    return -o1.getExperimentNo().compareTo(o2.getExperimentNo());
                }
            }
        );
        return eList;
    }

    @Override
    public List<Experiment> sampleList(Experiment experiment)
    {
        List<Experiment> experimentList = experimentMapper.selectExperimentSampleList(experiment);
//        Map<String,Experiment> eMap = new HashMap<String,Experiment>();
//        for(Experiment i : experimentList){
//            if(null == eMap.get(i.getId())){
//                eMap.put(i.getId(),i);
//                HashMap<String,ProcessParams> ppMap = new HashMap<String,ProcessParams>();
//                ProcessParams pp = new ProcessParams();
//                pp.setId(i.getProcessParamsId());
//                pp.setVjson(i.getPvjson());
//                pp.setCjson(i.getPcjson());
//                HashMap<String,Sample> sMap = new HashMap<String,Sample>();
//                Sample s = new Sample();
//                s.setCjson(i.getScjson());
//                s.setVjsonq(i.getSvjson());
//                s.setId(i.getSampleId());
//                s.setSampleNo(i.getSampleNo());
//                s.setName(i.getSampleName());
//                s.setRemark(i.getSampleRemark());
//                ArrayList<Sample> sampleArrayList= new ArrayList<Sample>();
//                sampleArrayList.add(s);
//                pp.setSampleList(sampleArrayList);
//                sMap.put(i.getSampleId(),s);
//                pp.setSampleMap(sMap);
//                ppMap.put(i.getProcessParamsId(),pp);
//                ArrayList<ProcessParams> processParamsArrayList= new ArrayList<ProcessParams>();
//                processParamsArrayList.add(pp);
//                i.setProcessMap(ppMap);
//                i.setProcessParamsList(processParamsArrayList);
//            }else{
//                Experiment e = eMap.get(i.getId());
//                HashMap<String,ProcessParams>pMap = e.getProcessMap();
//                if(null == pMap.get(i.getProcessParamsId())){
//                    ProcessParams pp = new ProcessParams();
//                    pp.setId(i.getProcessParamsId());
//                    pp.setProcessParamsNo(i.getProcessParamsNo());
//                    pp.setVjson(i.getPvjson());
//                    pp.setCjson(i.getPcjson());
//                    HashMap<String,Sample> sMap = new HashMap<String,Sample>();
//                    Sample s = new Sample();
//                    s.setCjson(i.getScjson());
//                    s.setVjsonq(i.getSvjson());
//                    s.setId(i.getSampleId());
//                    s.setSampleNo(i.getSampleNo());
//                    s.setName(i.getSampleName());
//                    s.setRemark(i.getSampleRemark());
//                    sMap.put(i.getSampleId(),s);
//                    pp.setSampleMap(sMap);
//                    ArrayList<Sample> sampleArrayList= new ArrayList<Sample>();
//                    sampleArrayList.add(s);
//                    pp.setSampleList(sampleArrayList);
//                    pMap.put(i.getProcessParamsId(),pp);
//                    e.setProcessMap(pMap);
//                    e.getProcessParamsList().add(pp);
//                }else{
//                    ProcessParams pp = pMap.get(i.getProcessParamsId());
//                    pp.setProcessParamsNo(i.getProcessParamsNo());
//                    HashMap<String,Sample>sMap = pp.getSampleMap();
//                    if(null == sMap.get(i.getSampleId())){
//                        Sample s = new Sample();
//                        s.setCjson(i.getScjson());
//                        s.setVjsonq(i.getSvjson());
//                        s.setId(i.getSampleId());
//                        s.setSampleNo(i.getSampleNo());
//                        s.setName(i.getSampleName());
//                        s.setRemark(i.getSampleRemark());
//                        sMap.put(i.getSampleId(),s);
////                        ArrayList<Sample> sampleArrayList= new ArrayList<Sample>();
////                        sampleArrayList.add(s);
//                        pp.getSampleList().add(s);
////                        pp.setSampleList(sampleArrayList);
//                    }else{
//                        // do nothing
//                    }
//                }
//            }
//        }
//        List<Experiment> eList = new ArrayList<Experiment>(eMap.values());
        return experimentList;
    }

    /**
     * 新增实验
     * 
     * @param experiment 实验
     * @return 结果
     */
    @Override
    public int insertExperiment(Experiment experiment)
    {
        experiment.setId(IdUtils.simpleUUID());
        experiment.setCreateTime(DateUtils.getNowDate());
        experiment.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        experiment.setExperimentNo(getNextExperimentNo(experiment));
        String jsonStr = experiment.getCjson();
        List<ProcessParams> paramList = JSONArray.parseArray(jsonStr, ProcessParams.class);
        for(ProcessParams i : paramList){
            i.setId(IdUtils.simpleUUID());
            experiment.setProcessParamsId(i.getId());
            i.setExperimentId(experiment.getId());
            i.setCreateBy(experiment.getCreateBy());
            i.setCreateTime(DateUtils.getNowDate());
            String nextpNo = getNextProcessParamsNo(experiment);
            i.setProcessParamsNo(nextpNo);
            for(Sample j:i.getSampleList()){
                j.setId(IdUtils.simpleUUID());
                j.setProcessParamsId(i.getId());
                j.setCreateBy(experiment.getCreateBy());
                j.setCreateTime(DateUtils.getNowDate());
                j.setExperimentId(experiment.getId());
                j.setSampleNo(getNextSampleNo(nextpNo,experiment));
                sampleMapper.insertSample(j);
            }
            processParamsMapper.insertProcessParams(i);
        }
        return experimentMapper.insertExperiment(experiment);
    }

    /**
     * 修改实验
     * 
     * @param experiment 实验
     * @return 结果
     */
    @Override
    public int updateExperiment(Experiment experiment)
    {
        experiment.setUpdateTime(DateUtils.getNowDate());
        experiment.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        String jsonStr = experiment.getCjson();
        List<ProcessParams> paramList = JSONArray.parseArray(jsonStr, ProcessParams.class);
        processParamsMapper.deleteProcessParamsByExperimentId(experiment.getId());
        sampleMapper.deleteSampleByExperimentId(experiment.getId());
        for(ProcessParams i : paramList){
            i.setId(IdUtils.simpleUUID());
            experiment.setProcessParamsId(i.getId());
            i.setExperimentId(experiment.getId());
            i.setCreateBy(experiment.getCreateBy());
            i.setCreateTime(DateUtils.getNowDate());
            String nextpNo = getNextProcessParamsNo(experiment);
            i.setProcessParamsNo(nextpNo);
            List<Sample> sampleList = i.getSampleList();
            for(Sample j:sampleList){
                j.setId(IdUtils.simpleUUID());
                j.setProcessParamsId(i.getId());
                j.setExperimentId(experiment.getId());
                j.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                j.setCreateTime(DateUtils.getNowDate());
                j.setSampleNo(getNextSampleNo(nextpNo,experiment));
                sampleMapper.insertSample(j);
            }
            processParamsMapper.insertProcessParams(i);
        }
        return experimentMapper.updateExperiment(experiment);
    }

    /**
     * 批量删除实验
     * 
     * @param ids 需要删除的实验主键
     * @return 结果
     */
    @Override
    public int deleteExperimentByIds(String[] ids)
    {
        for(String id:ids){
            processParamsMapper.deleteProcessParamsByExperimentId(id);
            sampleMapper.deleteSampleByExperimentId(id);
        }
        return experimentMapper.deleteExperimentByIds(ids);
    }

    /**
     * 删除实验信息
     * 
     * @param id 实验主键
     * @return 结果
     */
    @Override
    public int deleteExperimentById(String id)
    {
        return experimentMapper.deleteExperimentById(id);
    }

    private String getNextExperimentNo(Experiment experiment){
        Experiment result = experimentMapper.getNextExperimentNo(experiment);
        String process = experiment.getProcess();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(experiment.getExperimentTime());
        String nextSeq="01";
        if(null!=result){
            nextSeq = result.getExperimentNo();
        }
        return process+"-"+date+"-"+nextSeq;
    }

    private String getNextProcessParamsNo(Experiment experiment){
        Experiment result = experimentMapper.getNextProcessParamsNo(experiment);
        String nextSeq="01";
        if(null!=result){
            nextSeq = result.getProcessParamsNo();
        }
        return nextSeq;
    }

    private String getNextSampleNo(String pNo,Experiment experiment){
        Experiment result = experimentMapper.getNextSampleNo(experiment);
        String nextSeq="01";
        if(null!=result){
            nextSeq = result.getSampleNo();
        }
        return pNo+nextSeq;
    }
}
